/*
 * @Author: hongbin
 * @Date: 2023-03-31 14:18:22
 * @LastEditors: hongbin
 * @LastEditTime: 2023-03-31 14:27:53
 * @Description: 更新用户信息
 */
import { ObjectId } from "mongodb";
import { NextApiRequest, NextApiResponse } from "next";
import { VerifyFields } from "../../../src/api/utils";
import DB from "../mongodb";
import { receiveRequest } from "../utils";

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
    if (!receiveRequest(req, res, "POST")) return;
    const db = await DB;
    console.log(req.body);

    if (!VerifyFields.has(req.body, ["_id", "name"])) {
        return res.status(500).json("缺少关键字段");
    }
    const userInfo = { ...req.body };
    delete userInfo["_id"];

    const update = await db.collection("userInfo").updateOne({ _id: new ObjectId(req.body._id) }, { $set: userInfo });
    console.log(update);
    if (update.acknowledged) {
        res.status(200).json({ data: userInfo });
        return;
    }
    res.status(200).json({ data: null, msg: "更新失败", code: -1 });
}
